|
1
|
|
|
const utils = require("./utils.js"); |
|
2
|
|
|
const zip = require("gulp-zip"); |
|
3
|
|
|
const vinylPaths = require("vinyl-paths"); |
|
4
|
|
|
const del = require("del"); |
|
5
|
|
|
const composer = require("gulp-composer"); |
|
6
|
|
|
const fs = require("fs"); |
|
7
|
|
|
const { task, series, parallel, watch, dest, src } = require("gulp"); |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Main configuration |
|
11
|
|
|
* |
|
12
|
|
|
* @type {{defaultTasks: string[], releaseDir: string, wwwDir: string, watchInterval: number, useVersions: boolean}} |
|
13
|
|
|
*/ |
|
14
|
|
|
const config = { |
|
15
|
|
|
defaultTasks: ["release"], |
|
16
|
|
|
releaseDir: "./releases", |
|
17
|
|
|
wwwDir: "/.tmp/www", |
|
18
|
|
|
watchInterval: 500, |
|
19
|
|
|
useVersions: true, |
|
20
|
|
|
}; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Base tasks for every directory (to be cloned using getBaseTasks) |
|
24
|
|
|
* |
|
25
|
|
|
* @type {{release: Array, clean: Array, copy: Array, watch: Array}} |
|
26
|
|
|
*/ |
|
27
|
|
|
const baseDirSt = { |
|
28
|
|
|
release: [], |
|
29
|
|
|
clean: [], |
|
30
|
|
|
copy: [], |
|
31
|
|
|
watch: [], |
|
32
|
|
|
}; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Sets the configuration from a data object, overwriting it where possible |
|
36
|
|
|
* |
|
37
|
|
|
* @param data |
|
38
|
|
|
*/ |
|
39
|
|
|
function setConfigurationFromData(data) { |
|
40
|
|
|
if (data.hasOwnProperty("defaultTasks")) { |
|
41
|
|
|
config.defaultTasks = data.defaultTasks; |
|
42
|
|
|
} |
|
43
|
|
|
if (data.hasOwnProperty("releaseDir")) { |
|
44
|
|
|
config.releaseDir = data.releaseDir; |
|
45
|
|
|
} |
|
46
|
|
|
if (data.hasOwnProperty("wwwDir")) { |
|
47
|
|
|
config.wwwDir = data.wwwDir; |
|
48
|
|
|
} |
|
49
|
|
|
if (data.hasOwnProperty("watchInterval")) { |
|
50
|
|
|
config.watchInterval = data.watchInterval; |
|
51
|
|
|
} |
|
52
|
|
|
if (data.hasOwnProperty("useVersions")) { |
|
53
|
|
|
config.useVersions = data.useVersions; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Clones the base task set |
|
59
|
|
|
* |
|
60
|
|
|
* @returns {Object} |
|
61
|
|
|
*/ |
|
62
|
|
|
function getBaseTasks() { |
|
63
|
|
|
return utils.cloneObject(baseDirSt); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Clones only the web tasks |
|
68
|
|
|
* |
|
69
|
|
|
* @returns {Object} |
|
70
|
|
|
*/ |
|
71
|
|
|
function getBaseWebTasks() { |
|
72
|
|
|
const webTasks = utils.cloneObject(baseDirSt); |
|
73
|
|
|
delete webTasks.release; |
|
74
|
|
|
return webTasks; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Generates a full task set based on the baseDirSt object and the baseTask given |
|
79
|
|
|
* |
|
80
|
|
|
* @param {string} baseTask |
|
81
|
|
|
* @param {Object} taskSet Optional to populate an existing variable |
|
82
|
|
|
* @param {boolean} generateReleaseTasks Generate release tasks or not |
|
83
|
|
|
* @param {boolean} generateWebTasks Generate web tasks or not |
|
84
|
|
|
* |
|
85
|
|
|
* @returns {Array} |
|
86
|
|
|
*/ |
|
87
|
|
|
function generateTaskSet( |
|
88
|
|
|
baseTask, |
|
89
|
|
|
taskSet, |
|
90
|
|
|
generateReleaseTasks, |
|
91
|
|
|
generateWebTasks |
|
92
|
|
|
) { |
|
93
|
|
|
const tasks = taskSet === null ? getBaseTasks() : null; |
|
94
|
|
|
let destination = taskSet === null ? tasks : taskSet; |
|
95
|
|
|
|
|
96
|
|
|
if (generateReleaseTasks) { |
|
97
|
|
|
destination.release.push("release:" + baseTask); |
|
98
|
|
|
} else { |
|
99
|
|
|
delete destination.release; |
|
100
|
|
|
} |
|
101
|
|
|
if (generateWebTasks) { |
|
102
|
|
|
destination.clean.push("clean:" + baseTask); |
|
103
|
|
|
destination.copy.push("copy:" + baseTask); |
|
104
|
|
|
destination.watch.push("watch:" + baseTask); |
|
105
|
|
|
} else { |
|
106
|
|
|
delete destination.clean; |
|
107
|
|
|
delete destination.copy; |
|
108
|
|
|
delete destination.watch; |
|
109
|
|
|
} |
|
110
|
|
|
return destination; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Creates the gulp tasks given a base task and its dependencies, both following the base task structure |
|
115
|
|
|
* |
|
116
|
|
|
* @param {string} baseTask |
|
117
|
|
|
* @param {Object} dependencies |
|
118
|
|
|
* @param {Object|boolean} tasks Object with tasks to generate. If false it generates the default set |
|
119
|
|
|
*/ |
|
120
|
|
|
function createGulpTasks(baseTask, dependencies, tasks) { |
|
121
|
|
|
if (tasks === false || tasks.hasOwnProperty("release")) { |
|
122
|
|
|
task("release:" + baseTask, parallel(dependencies.release)); |
|
123
|
|
|
} |
|
124
|
|
|
if (tasks === false || tasks.hasOwnProperty("clean")) { |
|
125
|
|
|
task("clean:" + baseTask, parallel(dependencies.clean)); |
|
126
|
|
|
} |
|
127
|
|
|
if (tasks === false || tasks.hasOwnProperty("copy")) { |
|
128
|
|
|
task("copy:" + baseTask, parallel(dependencies.copy)); |
|
129
|
|
|
} |
|
130
|
|
|
if (tasks === false || tasks.hasOwnProperty("watch")) { |
|
131
|
|
|
task("watch:" + baseTask, parallel(dependencies.watch)); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Generates generic tasks for a given directory and returns them with their directories |
|
137
|
|
|
* |
|
138
|
|
|
* @param {string} mainDir Main directory where tasks reside (subfolders are getting their own tasks) |
|
139
|
|
|
* @param {string} baseTask Base task of the main directory (to be created with dependency on the sub directory tasks) |
|
140
|
|
|
* |
|
141
|
|
|
* @returns {Object} Collection of tasks and collection of directories, each with its own tasks |
|
142
|
|
|
*/ |
|
143
|
|
|
function generateDirTasks(mainDir, baseTask) { |
|
144
|
|
|
const mainTasks = getBaseTasks(); |
|
145
|
|
|
const directories = []; |
|
146
|
|
|
utils.getFilesByType(mainDir, "directory").forEach(function (dir) { |
|
147
|
|
|
const dirTasks = generateTaskSet( |
|
148
|
|
|
baseTask + ":" + dir, |
|
149
|
|
|
null, |
|
150
|
|
|
true, |
|
151
|
|
|
true |
|
152
|
|
|
); |
|
153
|
|
|
generateTaskSet(baseTask + ":" + dir, mainTasks, true, true); |
|
154
|
|
|
directories.push({ directory: dir, tasks: dirTasks }); |
|
155
|
|
|
}); |
|
156
|
|
|
return { tasks: mainTasks, directories: directories }; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Generates and creates the actual gulp tasks for a given directory, considering it an actual extension |
|
161
|
|
|
* |
|
162
|
|
|
* @param {string} dir Directory where content tasks are to be generated |
|
163
|
|
|
* @param {Array} extraSources Other source directories to include |
|
164
|
|
|
* @param {string} extensionName Extension name |
|
165
|
|
|
* @param {string} baseTask Base task name of this directory |
|
166
|
|
|
* @param {string} destinationWebDir Directory where this package belongs in the website |
|
167
|
|
|
* @param {string} destinationReleaseDir Directory where the release package will be moved to |
|
168
|
|
|
* @param {string} zipName Zip name to provide to the release package |
|
169
|
|
|
* |
|
170
|
|
|
* @returns {Object} Generated tasks dependent of the main task |
|
171
|
|
|
*/ |
|
172
|
|
|
function generateContentTasks( |
|
173
|
|
|
dir, |
|
174
|
|
|
extraSources, |
|
175
|
|
|
extensionName, |
|
176
|
|
|
baseTask, |
|
177
|
|
|
destinationWebDir, |
|
178
|
|
|
destinationReleaseDir, |
|
179
|
|
|
zipName |
|
180
|
|
|
) { |
|
181
|
|
|
// Checks which tasks must be generated. If dirs are omitted, tasks are too |
|
182
|
|
|
const executeWebTasks = destinationWebDir !== ""; |
|
183
|
|
|
const executeReleaseTasks = destinationReleaseDir !== ""; |
|
184
|
|
|
|
|
185
|
|
|
// Get out of here if no directories are provided |
|
186
|
|
|
if (!executeWebTasks && !executeReleaseTasks) { |
|
187
|
|
|
return {}; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
const releaseTasks = ["release-do:" + baseTask]; |
|
191
|
|
|
const copyTasks = ["copy-do:" + baseTask]; |
|
192
|
|
|
const watchTasks = ["watch-main:" + baseTask]; |
|
193
|
|
|
const composerExists = fs.existsSync(dir + "/composer.json"); |
|
194
|
|
|
const sources = [dir + "/**"].concat(extraSources); |
|
195
|
|
|
|
|
196
|
|
|
if (composerExists) { |
|
197
|
|
|
task("composer:" + baseTask, function () { |
|
198
|
|
|
return composer({ "working-dir": dir }); |
|
199
|
|
|
}); |
|
200
|
|
|
releaseTasks.unshift("composer:" + baseTask); |
|
201
|
|
|
copyTasks.unshift("composer:" + baseTask); |
|
202
|
|
|
watchTasks.unshift("watch-composer:" + baseTask); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
if (executeReleaseTasks) { |
|
206
|
|
|
// Release tasks |
|
207
|
|
|
const releaseFunction = function () { |
|
208
|
|
|
const versionNumber = |
|
209
|
|
|
config.useVersions === true |
|
210
|
|
|
? utils.getManifestVersion( |
|
211
|
|
|
dir + "/" + extensionName + ".xml" |
|
212
|
|
|
) |
|
213
|
|
|
: ""; |
|
214
|
|
|
const versionName = |
|
215
|
|
|
versionNumber !== "" ? "-v" + versionNumber : ""; |
|
216
|
|
|
return src(sources) |
|
217
|
|
|
.pipe(zip(zipName + versionName + ".zip")) |
|
218
|
|
|
.pipe(dest(config.releaseDir + "/" + destinationReleaseDir)); |
|
219
|
|
|
}; |
|
220
|
|
|
if (composerExists) { |
|
221
|
|
|
task("release-do:" + baseTask, releaseFunction); |
|
222
|
|
|
task("release:" + baseTask, parallel(releaseTasks)); |
|
223
|
|
|
} else { |
|
224
|
|
|
task("release:" + baseTask, releaseFunction); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
if (executeWebTasks) { |
|
229
|
|
|
// Clean task |
|
230
|
|
|
task("clean:" + baseTask, function () { |
|
231
|
|
|
return src(config.wwwDir + "/" + destinationWebDir, { |
|
232
|
|
|
allowEmpty: true, |
|
233
|
|
|
}).pipe( |
|
234
|
|
|
vinylPaths(function (paths) { |
|
235
|
|
|
del.sync(paths, { force: true }); |
|
236
|
|
|
return Promise.resolve(); |
|
237
|
|
|
}) |
|
238
|
|
|
); |
|
239
|
|
|
}); |
|
240
|
|
|
|
|
241
|
|
|
// Copy tasks |
|
242
|
|
|
task( |
|
243
|
|
|
"copy-do:" + baseTask, |
|
244
|
|
|
series("clean:" + baseTask, function () { |
|
245
|
|
|
return src(sources).pipe( |
|
246
|
|
|
dest(config.wwwDir + "/" + destinationWebDir) |
|
247
|
|
|
); |
|
248
|
|
|
}) |
|
249
|
|
|
); |
|
250
|
|
|
task("copy:" + baseTask, series(copyTasks)); |
|
251
|
|
|
|
|
252
|
|
|
// Watch tasks |
|
253
|
|
|
const watchFunction = function () { |
|
254
|
|
|
return watch( |
|
255
|
|
|
sources, |
|
256
|
|
|
{ interval: config.watchInterval }, |
|
257
|
|
|
series("copy-do:" + baseTask) |
|
258
|
|
|
); |
|
259
|
|
|
}; |
|
260
|
|
|
|
|
261
|
|
|
if (composerExists) { |
|
262
|
|
|
task("watch-composer:" + baseTask, function () { |
|
263
|
|
|
return watch( |
|
264
|
|
|
[dir + "/composer.json", dir + "/composer.lock"], |
|
265
|
|
|
{ interval: config.watchInterval }, |
|
266
|
|
|
series("composer:" + baseTask) |
|
267
|
|
|
); |
|
268
|
|
|
}); |
|
269
|
|
|
task("watch-main:" + baseTask, watchFunction); |
|
270
|
|
|
task("watch:" + baseTask, parallel(watchTasks)); |
|
271
|
|
|
} else { |
|
272
|
|
|
task("watch:" + baseTask, watchFunction); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return generateTaskSet( |
|
277
|
|
|
baseTask, |
|
278
|
|
|
null, |
|
279
|
|
|
executeReleaseTasks, |
|
280
|
|
|
executeWebTasks |
|
281
|
|
|
); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
exports.config = config; |
|
285
|
|
|
exports.baseDirSt = baseDirSt; |
|
286
|
|
|
exports.setConfigurationFromData = setConfigurationFromData; |
|
287
|
|
|
exports.getBaseTasks = getBaseTasks; |
|
288
|
|
|
exports.getBaseWebTasks = getBaseWebTasks; |
|
289
|
|
|
exports.generateDirTasks = generateDirTasks; |
|
290
|
|
|
exports.generateContentTasks = generateContentTasks; |
|
291
|
|
|
exports.createGulpTasks = createGulpTasks; |
|
292
|
|
|
|